use std::sync::mpsc::Receiver;

use crate::gen_title;

pub fn ch2_27_1() {
    gen_title("ch2_27_1", 18);

    // 我是Sun
    // face
    let name = "sunface";

    let age = 18; // 今年好像是18岁

    println!("{} is {} years old", name, age);
}

pub fn ch2_27_2() {
    gen_title("ch2_27_2", 18);

    /*
    我是
    Misiro
     */
    let name = "sunface";
    let age = "???"; // 今年其实不小了
}

/// `add_one` 将指定值加1
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = rust_learn::ch2::ch2_27::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {
    x + 1
}

pub fn ch2_27_3() {
    gen_title("ch2_27_3", 18);

    println!("{}", add_one(5));
}

/** `add_two` 将指定值加2

``` rust
     let arg = 5;
     let answer = rust_learn::ch2::ch2_27::add_two(arg);

     assert_eq!(7, answer);
```
 */
pub fn add_two(x: i32) -> i32 {
    x + 2
}

pub fn ch2_27_4() {
    gen_title("ch2_27_4", 18);

    println!("{}", add_two(5));
}

/// # Panics
///
/// The function panics if the second argument is zero.
///
/// ``` rust,should_panic
/// // panics on division by zero
/// use rust_learn::ch2_27::div;
/// div(10,0);
/// ```
pub fn div(a: i32, b: i32) -> i32 {
    if b == 0 {
        panic!("Divide-by-zero error");
    }

    a / b
}

/// ``` rust,should_panic
/// # // 使用#开头的行会在文档中被隐藏起来，但是依然会在文档测试中运行
/// # fn try_main() -> Result<(), String> {
/// # use rust_learn::ch2_27::try_div;
///  let res = try_div(10, 0)?;
/// # Ok(()) // returning from try_main
/// # }
/// # fn main() {
/// #    try_main().unwrap();
/// #
/// # }
/// ```
pub fn try_div(a: i32, b: i32) -> Result<i32, String> {
    if b == 0 {
        Err(String::from("Divide-by-zero"))
    } else {
        Ok(a / b)
    }
}

/// [`Receiver<T>`] [`std::future`]
///
/// [`std::future::Future`] [`Self::recv()`].
pub struct AsyncReceiver<T> {
    sender: Receiver<T>,
}

impl<T> AsyncReceiver<T> {
    pub async fn recv() -> T {
        unimplemented!();
    }
}
